use sqlx::{Error,Pool,Postgres};
// 这是当不需要返回插&入数据结果时使用的sqlx类型
//use sqlx::postgres::PgQueryResult;

use crate::types::user_types::{User};

/// 插入一条新的用户数据，返回对应的结构体
pub async fn create_user(pool: &Pool<Postgres>, phone: &str, password: &String, token:&String, time:&i64, conuntry:&String) -> Result<User, Error> {
    println!("token:{}",token);
    let nickname = "用户".to_string() + &time.to_string();
    let user = User{
        id: 0,
        nickname:nickname,
        password:password.to_string(),
        token:token.to_string(),
        avatar:"https://img2.baidu.com/it/u=100890290,112861643&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500".to_string(),
        state:0,
        soft_delete: 0,
        follow_len:0,
        like_len:0,
        desec: "ta暂时还没自我介绍,给点时间耐心等待吧~".to_string(),
        creator_id:0,
        withdrawal:0,
        payable:0,
        pending_cash:0,
        balance:0,
        country:conuntry.to_string(),
        phone:phone.to_string(),
        real_name:"name".to_string(),
        id_code:"000000000000000000".to_string(),
        is_adult:0,
        background:None,
        role_id:1,
        add_time: *time,
    };
    let r = sqlx::query_as(
        r#"
            INSERT INTO public.user (
                nickname,
                password,
                token,
                avatar,
                state,
                soft_delete,
                follow_len,
                like_len,
                desec,
                creator_id,
                withdrawal,
                payable,
                pending_cash,
                balance,
                country,
                phone,
                real_name,
                id_code,
                is_adult,
                role_id,
                add_time
            )
                VALUES 
            ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)  
                RETURNING 
                id,
                nickname,
                password,
                token,
                avatar,
                state,
                soft_delete,
                follow_len,
                like_len,
                desec,
                creator_id,
                withdrawal,
                payable,
                pending_cash,
                balance,
                country,
                phone,
                real_name,
                id_code,
                is_adult,
                background,
                role_id,
                add_time
        "#,
    )
        .bind(user.nickname)
        .bind(user.password)
        .bind(user.token)
        .bind(user.avatar)
        .bind(user.state)
        .bind(user.soft_delete)
        .bind(user.follow_len)
        .bind(user.like_len)
        .bind(user.desec)
        .bind(user.creator_id)
        .bind(user.withdrawal)
        .bind(user.payable)
        .bind(user.pending_cash)
        .bind(user.balance)
        .bind(user.country)
        .bind(user.phone)
        .bind(user.real_name)
        .bind(user.id_code)
        .bind(user.is_adult)
        .bind(user.role_id)
        .bind(user.add_time)
        .fetch_one(pool)
        .await?;
        
    Ok(r)
}